package web.services;

import web.dao.SearchDao;
import web.util.RegexUtil;
import web.util.StringOperateUtil;
import main.FindData;
import org.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2016/9/27.
 */
@Service
public class AdminService {
    @Autowired
    private LuceneSearchService luceneSearchService;
    @Autowired
    private SearchDao searchDao;
    @Autowired
    private ToolService toolService;

    /**
     * 通过关键词查询出招标公司
     * @param key
     * @return
     */
    public List<String> findSubscriberByKey(String key){
        //        招标单位
        List<String> listSubscriber = new ArrayList<String>();
        int tableNum = 65;
        do {
//            查询所有招标单位
            List<String> list = new ArrayList<String>();

            try {
                list = searchDao.findSubscriberByKey(key, "tender_news_info_" + tableNum);
            }catch (Exception e){
                break;
            }
//            去除重复
            for (String s : list) {
                if (listSubscriber.contains(s)) {
                    continue;
                }
                listSubscriber.add(s);
            }
            tableNum++;
        }while (true);
        return listSubscriber;
    }

    /**
     * 方法一：直接查询
     * 通过招标单位查询该单位近一年的信息标题
     * @param subscriber
     * @return
     */
    public List<String> findTitleByKey(String subscriber){
//        研发数据库招标表明
        List<String> tables = searchDao.findTable("tender_news_info_%");
//        该招标单位其他标题信息
        List<String> listTitle = new ArrayList<String>();
        for (String table : tables) {
//        查询招标公司相关的标题
            listTitle.addAll(searchDao.findTitleBySubscriber(subscriber, table));
        }
        return listTitle;
    }

    /**
     * 方法二：查询缓存
     * 通过招标单位查询该单位近一年的信息标题
     * @param subscriber
     * @return
     */
    public List<String> findTitleByKeys(String subscriber){
//        通过招标单位查询信息
        List<Map<String,Object>> list = toolService.findAllSubscriberContactBySubscriber(subscriber);
//        返回标题
        List<String> listTitle = new ArrayList<String>();
        for (Map<String, Object> map : list) {
            String title_key = map.get("title_key").toString();
            listTitle.add(StringOperateUtil.recoveryFc(title_key));
        }
        return listTitle;
    }

    public static void main(String[] args) {
        String content="南昌 昌龙 龙头 头岗 岗综 综合 合码 码头 头一 一期 期集 集装 装箱 箱牵 牵引 引车 车及 及集 集装 装箱 箱半 半挂 挂车 e9 标 第四 四次 ";
        System.out.println(Client.fc(StringOperateUtil.recoveryFc(content)));
    }


    /**
     * 通过关键词查询前序关键词
     * @param key
     * @return
     */
    public List<Map<String,Object>> forecastService(String key){
        if(key==null || key.isEmpty()){
            return new ArrayList<Map<String, Object>>();
        }
//        通过关键词获取所有招标单位
        List<String> listSubscriber = findSubscriberByKey(key);
//        招标信息标题
        List<String> listTitle = new ArrayList<String>();
//        查询所有招标单位对应的标题
        for (String subscriber : listSubscriber) {
            listTitle.addAll(findTitleByKeys(subscriber));
        }
        Map<String,Object> map = new HashMap<String, Object>();
//       将标题分词并获得出现次数
        for (String title : listTitle) {
//           词性分成
            String [] keys = RegexUtil.getWords(
                    FindData.getTitleKey(
                            StringOperateUtil.reStrings(title)),"/n");
            for (String s : keys) {
                if(s.length() < 2 ){
                    continue;
                }
                if(map.get(s)==null){
                    map.put(s , 1);
                }else{
                    map.put(s ,Integer.parseInt(map.get(s).toString())+1);
                }
            }
        }
//        将map转换为List
        List<Map<String,Object>> forecastKeyList = new ArrayList<Map<String, Object>>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Map<String,Object> mapNews = new HashMap<String, Object>();
            mapNews.put("key",entry.getKey());
            mapNews.put("num",entry.getValue());
            forecastKeyList.add(mapNews);
        }
//        按num排序
        Collections.sort(forecastKeyList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return Integer.parseInt(o2.get("num").toString()) - Integer.parseInt(o1.get("num").toString());
            }
        });

        return forecastKeyList;
    }
}
